<template>
	<el-dialog title="角色权限设置" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-tabs tab-position="top">
			<el-tab-pane label="菜单权限">
				<div class="treeMain">
					<el-tree ref="menu" node-key="id" :data="menu.list" :props="menu.props" show-checkbox></el-tree>
				</div>
			</el-tab-pane>
			<!--
			<el-tab-pane label="数据权限">
				<el-form label-width="100px" label-position="left">
					<el-form-item label="规则类型">
						<el-select v-model="data.dataType" placeholder="请选择">
							<el-option label="全部可见" value="1"></el-option>
							<el-option label="所在部门及子级可见" value="4"></el-option>
							<el-option label="所在部门可见" value="3"></el-option>
							<el-option label="本人可见" value="2"></el-option>
						</el-select>
					</el-form-item>
				</el-form>
			</el-tab-pane>
			-->
		</el-tabs>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				visible: false,
				isSaveing: false,
				menu: {
					list: [],
					checked: [],
					props: {
						label: (data)=>{
							return data.meta.title
						}
					}
				},
				grid: {
					list: [],
					checked: ["welcome", "ver", "time", "progress", "echarts", "about"],
					props: {
						label: (data)=>{
							return data.title
						},
						disabled: (data)=>{
							return data.isFixed
						}
					}
				},
				data: {
					dataType :1,
					list: [],
					checked: [],
					props: {},
					rule: ""
				},
				role: {
					id: 0
				}
			}
		},
		mounted() {
			this.getMenu()
		},
		methods: {
			open(){
				this.visible = true;
				return this
			},
			async submit(){
				this.isSaveing = true;

				//选中的和半选的合并后传值接口
				var checkedKeys = this.$refs.menu.getCheckedKeys().concat(this.$refs.menu.getHalfCheckedKeys())
				console.log(checkedKeys)
				var postData = {
					roleId: this.role.id,
					resourceIds: checkedKeys
				}
				var res = await this.$API.system.role.setRes.exec(postData)
				if(res.code === 200){
					this.isSaveing = false;
					this.visible = false;
					this.$message.success("操作成功")
					this.$emit('success')
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async getMenu(){
				var res = await this.$API.system.menu.list.get()
				this.menu.list = res.data
				//获取接口返回的之前选中的和半选的合并，处理过滤掉有叶子节点的key
				res = await this.$API.system.role.getRes.exec({id: this.role.id})
				this.menu.checked = res.data
				this.$nextTick(() => {
					let filterKeys = this.menu.checked.filter(key => this.$refs.menu.getNode(key).isLeaf)
					this.$refs.menu.setCheckedKeys(filterKeys, true)
				})
			},
			setData(data){
				Object.assign(this.role, data)
			}
		}
	}
</script>

<style scoped>
	.treeMain {height:280px;overflow: auto;border: 1px solid #dcdfe6;margin-bottom: 10px;}
</style>
